<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
            <el-form-item label="供应商名称" prop="providerId">
                <el-select
                        v-model="queryParams.providerId"
                        placeholder="请选择供应商名称"
                        clearable
                        size="small"
                        style="width:180px"
                >
                    <el-option
                            v-for="dict in providerOptions"
                            :key="dict.providerId"
                            :label="dict.providerName"
                            :value="dict.providerId"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="申请人" prop="applyUserName">
                <el-input
                        v-model="queryParams.applyUserName"
                        placeholder="请输入申请人"
                        clearable
                        size="small"
                        style="width:180px"
                />
            </el-form-item>
            <el-form-item label="单据状态" prop="status">
                <el-select
                        v-model="queryParams.status"
                        placeholder="单据状态"
                        clearable
                        size="small"
                        style="width:180px"
                >
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->

        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleToNewPurchase">新增采购</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="this.SubConPass === 'y'? false :true" @click="handleDoAudit">提交审核</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="this.SubConPass === 'y'? false :true" @click="handleDoInvalid">作废</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="this.SubConPass === 'y'? false :true" @click="handleDoInventory">提交入库</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->
<!--"'/stock/editPurchase/'+scope.row.purchaseId""{name:'editPurchase',purId:{purchaseId:scope.row.purchaseId}}"-->
        <!-- 数据表格开始 -->
        <el-table border :data="purchaseTableList" @selection-change="handleSelectionChnage">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="单据ID" align="center" width="200" prop="purchaseId">
                <template slot-scope="scope">
                    <router-link :to="{name:'editPurchase',params:{purchaseId:scope.row.purchaseId}}" class="link-type">
                        <span>{{ scope.row.purchaseId }}</span>
                    </router-link>
                </template>
            </el-table-column><!--:formatter="providerFormatter"-->
            <el-table-column label="供应商" width="180" align="center" prop="providerId" :formatter="providerFormatter" />
            <el-table-column label="采购批发总额" align="center" prop="purchaseTradeTotalAmount">
                <template slot-scope="scope">
                    <span>{{ scope.row.purchaseTradeTotalAmount|rounding }}</span>
                </template>
            </el-table-column>
            <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
            <el-table-column label="申请人" align="center" prop="applyUserName" />
            <el-table-column label="入库操作人" align="center" prop="storageOptUser" />
            <el-table-column label="入库时间" width="180" align="center" prop="storageOptTime" />
            <el-table-column label="审核信息" align="center" prop="examine" />
            <el-table-column label="创建时间" width="180" align="center" prop="createTime" />
        </el-table>
        <!-- 数据表格结束 -->
        <!-- 分页控件开始 -->
        <el-pagination
                v-show="total>0"
                :current-page="currentPage"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->
    </div>
</template>

<script>
    export default {
        name: "purchase",
        // 过滤器
        filters: {
            // 保留两位小数
            rounding(value) {
                return value.toFixed(2)
            }
        },
        data(){
            return{
                //存放 按钮是否显示 数据
                SubConPass: "",
                // 分页数据总条数
                total: 0,
                currentPage:1,
                pageSize:5,
                // 字典表格数据
                purchaseTableList:[],
                // 状态数据字典
                statusOptions:[],
                // 供应商的数据
                providerOptions:[],
                // 查询参数
                queryParams: {
                    providerId: undefined,
                    applyUserName: undefined,
                    status: undefined
                },
                // 存放 点击事件 数据 提交审核
                StorageData: [],
                //存放提交状态
                auditStatus: "",
                // 单据号 存放
                purchaseId: "",
            }
        },
        created() {
            //可用供应商
            this.$axios.post("/stock/prov/prov/selectAllProvName/").then(result=>{
                this.providerOptions=result.data.data;
            })
            // 使用全局的根据字典类型查询字典数据的方法查询字典数据
            this.$axios.get("/stock/prod/dict/data/findByType/his_order_status").then(result => {
                this.statusOptions = result.data.data;
            })
            // 查询所有可用的生产厂家
            this.$axios.get("/stock/prod/prod/selectAllProdName/").then(res => {
                this.producerOptions = res.data.data;
            })
            //数据表格
            this.initPurchase()
        },
        methods:{
            //状态字典的解析
            statusFormatter(row){
                return this.formatDict(this.statusOptions,row.status)
            },
            // 翻译供应商
            providerFormatter(row) {
                let name = ''
                this.providerOptions.filter(item => {
                    if (parseInt(row.providerId) === parseInt(item.providerId)) {
                        name = item.providerName
                    }
                })
                return name
            },
            //数据表格
            initPurchase(){
              this.$axios.post("/stock/pur/pur/purchaseAll/"+this.currentPage+"/"+this.pageSize,this.queryParams).then(result=>{
                  this.purchaseTableList=result.data.data.records;
                  this.total=result.data.data.total;
              })
            },
            // 提交审核
            handleDoAudit() {
                // 提交审核
                this.auditStatus = 2
                this.publicState()
                this.initPurchase();
            },
            // 作废
            handleDoInvalid(row) {
                //作废状态
                this.auditStatus = 5
                this.publicState()
                this.initPurchase();
            },
            // 执行入库
            handleDoInventory(row) {
                // 提交入库
                this.auditStatus = 6
                this.publicState()
                // 刷新
                this.initPurchase();
            },
            //公共 修改 状态的方法
            publicState() {
                //提交 页面状态 审核
                this.$axios.post("/stock/pur/pur/SubConPass?status=" + this.auditStatus, this.StorageData).then(
                    res => {
                        if (res.data.data == -1) {
                            this.$message({
                                message: res.data.msg,
                                type: "warning"
                            });
                        } else if (res.data.data == 0) {
                            this.$message({
                                message: res.data.msg,
                                type: "error"
                            });
                        } else {
                            this.$message({
                                message: res.data.msg,
                                type: "success"
                            });
                        }
                        this.initPurchase()
                    })
            },
            // 跳转到新增采购的路由页面
            async handleToNewPurchase() {
                await this.$axios.get("/stock/pur/pur/NewPurQuery/").then(res=>{
                    //存放
                    this.purchaseId=res.data.data;
                })
                this.$router.push({
                    name:'newPurchase',
                    params:{
                        encryptionPurchaseId:this.purchaseId,
                    }
                })
            },
            // 条件查询
            handleQuery() {
                this.currentPage=1
                this.initPurchase()
            },
            // 重置查询条件
            resetQuery() {
                this.resetForm('queryForm')
                this.dateRange = []
                this.initPurchase()
            },
            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.pageSize = val
                // 重新查询
                this.initPurchase()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.currentPage = val
                // 重新查询
                this.initPurchase()
            },
            // 翻译生产厂家
            producterFormatter(row) {
                let name = ''
                this.producerOptions.filter(item => {
                    if (parseInt(row.producerId) === parseInt(item.producerId)) {
                        name = item.producerName
                    }
                })
                return name
            },
            // 数据表格的多选择框选择时触发
            handleSelectionChnage(row) {
                this.StorageData = row
                //
                if (row.length == 1) {
                    this.SubConPass = 'y'
                } else {
                    this.SubConPass = 'n'
                }
            },
        }
    }
</script>

<style scoped>

</style>
